package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.POIUtils;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import com.itheima.vo.OrderSettingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Autowired
    private OrdersettingService ordersettingService;

    /**
     * 根据年月查询对应的预约设置信息
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        log.info("根据年月查询对应的预约设置信息，{}", month);
        try {
            List<OrderSettingVO> orderSettingVOList = ordersettingService.getOrderSettingByMonth(month);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, orderSettingVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL, null);
        }
    }

    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        log.info("根据日期修改可预约人数，{}", orderSetting);
        try {
            ordersettingService.editNumberByOrderDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL, null);
        }
    }


    /**
     * 模板下载
     * @param filename
     * @param response
     * @return
     */
    @GetMapping("/download")
    public String download(String filename, HttpServletResponse response){
        log.info("模板下载，{}", filename);
        try {
            ordersettingService.download(filename, response);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return "模板下载失败";
        }
    }


    /**
     * 文件上传
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        log.info("预约设置文件上传，{}", excelFile.getOriginalFilename());
        try {
            ordersettingService.upload(excelFile);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL, null);
        }
    }
}
